package service

import (
	"github.com/astaxie/beego/orm"
	"pq/helper"
	"pq/repository"
	"strconv"
)

var areaRepository = &repository.AreaRepository{}
var treemaker = helper.DeepTree{}
type AreaService struct {
}

func (as AreaService) GetProvince() []orm.Params {
	return areaRepository.GetProvinces()
}

func (as AreaService) GetCity(province string) []orm.Params {
	return areaRepository.GetCity(province)
}

func (as AreaService) GetAll() []helper.Tree {
	all := areaRepository.GetProvinceWithCity()
	tree := []helper.Tree{}
	for _,val := range all {
		id,_ := strconv.Atoi(val["area_code"].(string))
		pid,_ := strconv.Atoi(val["parent_code"].(string))
		tree = append(tree, helper.Tree{
			Id : id,
			Pid : pid,
			Title: val["area_name"].(string),
			Children: nil,
		})
	}
	return treemaker.Make(tree,0)
}
